-- Sub Class Map --
Game.RPG.Map = {}
Game.RPG.Map.Maps = {}
-------------------------------------------
function in_table(t, n)
   local i
   i = 1
   while t[i] do
      if ( t[i] == n ) then
         return 1
      end
      i = i + 1
   end
   return 0
end
-------------------------------------------
Game.RPG.Menu.init ()

function Game.RPG.Map.newMap (id, mapFile, tileW, tileH, mapW, mapH, tilesImg, nonPassableTiles)
  Game.RPG.Map.Maps[id] = {}
  Game.RPG.Map.Maps[id].tileW = tileW
  Game.RPG.Map.Maps[id].tileH = tileH
  Game.RPG.Map.Maps[id].mapW = mapW
  Game.RPG.Map.Maps[id].mapH = mapH
  Game.RPG.Map.Maps[id].mapFile = mapFile
  Game.RPG.Map.Maps[id].tilesImg = Image.load (tilesImg, RAM)
  Game.RPG.Map.Maps[id].map = ScrollMap.new (Game.RPG.Map.Maps[id].tilesImg, mapFile, mapW, mapH, tileW, tileH)
  Game.RPG.Map.Maps[id].aIddleFrame = {Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].anims[1][2],
                                                        Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].anims[2][2],
                                                        Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].anims[3][2],
                                                        Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].anims[4][2]}
  Game.RPG.Map.Maps[id].nonPassableTiles = nonPassableTiles
  Game.RPG.Map.Maps[id].TSX = 0
  Game.RPG.Map.Maps[id].TSY = 0
  Game.RPG.Map.Maps[id].TSMax_X = (mapW - (SCREEN_WIDTH / tileW)) * tileW
  Game.RPG.Map.Maps[id].TSMax_Y = (mapH - (SCREEN_HEIGHT / tileH)) * tileH
  Game.RPG.Map.Maps[id].CenterW = 40
  Game.RPG.Map.Maps[id].CenterH = 40
  Game.RPG.Map.Maps[id].XLeftCenter = (SCREEN_WIDTH - Game.RPG.Map.Maps[id].CenterW) / 2
  Game.RPG.Map.Maps[id].XRightCenter = (SCREEN_WIDTH - Game.RPG.Map.Maps[id].CenterW) / 2 + Game.RPG.Map.Maps[id].CenterW
  Game.RPG.Map.Maps[id].YUpCenter = (SCREEN_HEIGHT - Game.RPG.Map.Maps[id].CenterH) / 2
  Game.RPG.Map.Maps[id].YDownCenter = (SCREEN_HEIGHT - Game.RPG.Map.Maps[id].CenterH) / 2 + Game.RPG.Map.Maps[id].CenterH
  Game.RPG.Map.Maps[id].Inc_X = 0
  Game.RPG.Map.Maps[id].Inc_Y = 0
  Game.RPG.Map.Maps[id].PrevX = 0
  Game.RPG.Map.Maps[id].PrevY = 0
  Game.RPG.Map.Maps[id].PrevTSX = 0
  Game.RPG.Map.Maps[id].PrevTSY = 0
  Game.RPG.Map.Maps[id].WhichTileX = 0
  Game.RPG.Map.Maps[id].WhichTileY = 0
end
function Game.RPG.Map.testPassable (id)
  if in_table (Game.RPG.Map.Maps[id].nonPassableTiles,
  ScrollMap.getTile(Game.RPG.Map.Maps[1].map,
  Game.RPG.Map.Maps[id].WhichTileX,
  Game.RPG.Map.Maps[id].WhichTileY)) == 1 then
    return 0
  end
  return 1
end
function Game.RPG.Map.initControls (id) 
  Game.RPG.Map.Maps[id].Inc_X = 0
  Game.RPG.Map.Maps[id].Inc_Y = 0
  if not Game.RPG.Menu.isVisible then
    if Keys.held.Up then
      Game.RPG.Map.Maps[id].Inc_Y = -Game.RPG.Database.HeroBase.speed
      Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir = 2
    end
    if Keys.held.Down then
      Game.RPG.Map.Maps[id].Inc_Y = Game.RPG.Database.HeroBase.speed
      Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir = 4
    end
    if Keys.held.Left then
      Game.RPG.Map.Maps[id].Inc_X = -Game.RPG.Database.HeroBase.speed
      Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir = 3
    end
    if Keys.held.Right then
      Game.RPG.Map.Maps[id].Inc_X = Game.RPG.Database.HeroBase.speed
      Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir = 1
    end
  end
  Game.RPG.Map.Maps[id].PrevX = Game.RPG.Database.HeroBase.x
  Game.RPG.Map.Maps[id].PrevY = Game.RPG.Database.HeroBase.y
  Game.RPG.Map.Maps[id].PrevTSX = Game.RPG.Map.Maps[id].TSX
  Game.RPG.Map.Maps[id].PrevTSY = Game.RPG.Map.Maps[id].TSY
  local IsOnCenterX = 0
  local IsOnCenterY = 0
  if Game.RPG.Database.HeroBase.x > Game.RPG.Map.Maps[id].XLeftCenter and Game.RPG.Database.HeroBase.x < Game.RPG.Map.Maps[id].XRightCenter then
    IsOnCenterX = 1
  end
  if Game.RPG.Database.HeroBase.y > Game.RPG.Map.Maps[id].YUpCenter and Game.RPG.Database.HeroBase.y< Game.RPG.Map.Maps[id].YDownCenter then
    IsOnCenterY = 1
  end
  local DoScroll = 0
  if Game.RPG.Map.Maps[id].Inc_X < 0 then
    if IsOnCenterX == 0 or Game.RPG.Map.Maps[id].TSX == 0 then
      Game.RPG.Database.HeroBase.x = Game.RPG.Database.HeroBase.x + Game.RPG.Map.Maps[id].Inc_X
    else
      Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].TSX + Game.RPG.Map.Maps[id].Inc_X
      DoScroll = 1
    end
  end
  if Game.RPG.Map.Maps[id].Inc_X > 0 then
    if IsOnCenterX == 0 or Game.RPG.Map.Maps[id].TSX == Game.RPG.Map.Maps[id].TSMax_X then
      Game.RPG.Database.HeroBase.x = Game.RPG.Database.HeroBase.x + Game.RPG.Map.Maps[id].Inc_X
    else
      Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].TSX + Game.RPG.Map.Maps[id].Inc_X
      DoScroll = 1
    end
  end
  if Game.RPG.Map.Maps[id].Inc_Y < 0 then
    if IsOnCenterY == 0 or Game.RPG.Map.Maps[id].TSY == 0 then
      Game.RPG.Database.HeroBase.y = Game.RPG.Database.HeroBase.y + Game.RPG.Map.Maps[id].Inc_Y
    else
      Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].TSY + Game.RPG.Map.Maps[id].Inc_Y
      DoScroll = 1
    end
  end
  if Game.RPG.Map.Maps[id].Inc_Y > 0 then
    if IsOnCenterY == 0 or Game.RPG.Map.Maps[id].TSY == Game.RPG.Map.Maps[id].TSMax_Y then
      Game.RPG.Database.HeroBase.y = Game.RPG.Database.HeroBase.y + Game.RPG.Map.Maps[id].Inc_Y
    else
      Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].TSY + Game.RPG.Map.Maps[id].Inc_Y
      DoScroll = 1
    end
  end
  if Game.RPG.Database.HeroBase.x < 0 then 
    Game.RPG.Database.HeroBase.x = 0 
  end
  if Game.RPG.Database.HeroBase.x > (SCREEN_WIDTH - Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w) then        
    Game.RPG.Database.HeroBase.x = SCREEN_WIDTH - Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w
  end
  if (Game.RPG.Database.HeroBase.y < 0) then  
    Game.RPG.Database.HeroBase.y = 0
  end
  if Game.RPG.Database.HeroBase.y > (SCREEN_HEIGHT - Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h) then  
    Game.RPG.Database.HeroBase.y = SCREEN_HEIGHT - Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h
  end
  if Game.RPG.Map.Maps[id].TSY < 0 then  
    Game.RPG.Map.Maps[id].TSY = 0 
  end
  if Game.RPG.Map.Maps[id].TSY > Game.RPG.Map.Maps[id].TSMax_Y then  
    Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].TSMax_Y
  end
  if Game.RPG.Map.Maps[id].TSX < 0 then
    Game.RPG.Map.Maps[id].TSX = 0
  end
  if Game.RPG.Map.Maps[id].TSX > Game.RPG.Map.Maps[id].TSMax_X then  
    Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].TSMax_X
  end
  Game.RPG.Map.Maps[id].WhichTileX = math.floor((Game.RPG.Map.Maps[id].TSX + Game.RPG.Database.HeroBase.x + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w/2)) / Game.RPG.Map.Maps[id].tileW)
  Game.RPG.Map.Maps[id].WhichTileY = math.floor((Game.RPG.Map.Maps[id].TSY + Game.RPG.Database.HeroBase.y + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h/4)) / Game.RPG.Map.Maps[id].tileH)
  if (Game.RPG.Map.testPassable(id) == 0) then
    Game.RPG.Database.HeroBase.x = Game.RPG.Map.Maps[id].PrevX
    Game.RPG.Database.HeroBase.y = Game.RPG.Map.Maps[id].PrevY
    Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].PrevTSX
    Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].PrevTSY
  end
  Game.RPG.Map.Maps[id].WhichTileX = math.floor((Game.RPG.Map.Maps[id].TSX + Game.RPG.Database.HeroBase.x + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w/2)) / Game.RPG.Map.Maps[id].tileW)
  Game.RPG.Map.Maps[id].WhichTileY = math.floor((Game.RPG.Map.Maps[id].TSY + Game.RPG.Database.HeroBase.y + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h/2) + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h/4)) / Game.RPG.Map.Maps[id].tileH)
  if (Game.RPG.Map.testPassable(id) == 0) then
    Game.RPG.Database.HeroBase.x = Game.RPG.Map.Maps[id].PrevX
    Game.RPG.Database.HeroBase.y = Game.RPG.Map.Maps[id].PrevY
    Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].PrevTSX
    Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].PrevTSY
  end
  Game.RPG.Map.Maps[id].WhichTileX = math.floor((Game.RPG.Map.Maps[id].TSX + Game.RPG.Database.HeroBase.x + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w/4)) / Game.RPG.Map.Maps[id].tileW)
  Game.RPG.Map.Maps[id].WhichTileY = math.floor((Game.RPG.Map.Maps[id].TSY + Game.RPG.Database.HeroBase.y + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h/2)) / Game.RPG.Map.Maps[id].tileH)
  if (Game.RPG.Map.testPassable(id) == 0) then
    Game.RPG.Database.HeroBase.x = Game.RPG.Map.Maps[id].PrevX
    Game.RPG.Database.HeroBase.y = Game.RPG.Map.Maps[id].PrevY
    Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].PrevTSX
    Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].PrevTSY
  end
  Game.RPG.Map.Maps[id].WhichTileX = math.floor((Game.RPG.Map.Maps[id].TSX + Game.RPG.Database.HeroBase.x + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w/2) + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].w/4)) / Game.RPG.Map.Maps[id].tileW)
  Game.RPG.Map.Maps[id].WhichTileY = math.floor((Game.RPG.Map.Maps[id].TSY + Game.RPG.Database.HeroBase.y + (Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].h/2)) / Game.RPG.Map.Maps[id].tileH)
  if (Game.RPG.Map.testPassable(id) == 0) then
    Game.RPG.Database.HeroBase.x = Game.RPG.Map.Maps[id].PrevX
    Game.RPG.Database.HeroBase.y = Game.RPG.Map.Maps[id].PrevY
    Game.RPG.Map.Maps[id].TSX = Game.RPG.Map.Maps[id].PrevTSX
    Game.RPG.Map.Maps[id].TSY = Game.RPG.Map.Maps[id].PrevTSY
  end
  if DoScroll == 1 then
    ScrollMap.scroll(Game.RPG.Map.Maps[id].map, Game.RPG.Map.Maps[id].TSX, Game.RPG.Map.Maps[id].TSY)
  end
end
function Game.RPG.Map.draw (id, screen)
  ScrollMap.draw(screen, Game.RPG.Map.Maps[id].map)
  if Game.RPG.Map.Maps[id].Inc_X == 0 and Game.RPG.Map.Maps[id].Inc_Y == 0 then
    Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].sprite:drawFrame(screen, Game.RPG.Database.HeroBase.x, Game.RPG.Database.HeroBase.x, Game.RPG.Map.Maps[id].aIddleFrame[Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir])
  else
    Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].sprite:playAnimation(screen, Game.RPG.Database.HeroBase.x, Game.RPG.Database.HeroBase.x, Game.RPG.Database.Characters[Game.RPG.Database.HeroBase.id].dir)
  end
  if RPG_MENU_ACTIVE then
    Game.RPG.Menu.draw ()
    if Keys.newPress.Start then
      if Game.RPG.Menu.isVisible then
        Game.RPG.Menu.hide ()
      else
        Game.RPG.Menu.show ()
      end
    end
  end
end
function Game.RPG.Map.destroy (id)
  Image.destroy (Game.RPG.Map.Maps[id].tilesImg)
  Game.RPG.Map.Maps[id].tilesImg = nil
  ScrollMap.destroy (Game.RPG.Map.Maps[id].map)
  Game.RPG.Map.Maps[id].map = nil
  Game.RPG.Map.Maps[id] = {}
end
-- end Map --